#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
简化版网站访问测试
"""

import sys
import asyncio
import logging
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from playwright.async_api import async_playwright


async def simple_test():
    """简化测试"""
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        
        try:
            # 测试基本访问
            logger.info("测试基本页面访问...")
            response = await page.goto("https://sz.pupuwang.com/siting/list", timeout=60000)
            logger.info(f"状态码: {response.status}")
            
            if response.status == 200:
                await page.wait_for_load_state('networkidle', timeout=30000)
                title = await page.title()
                logger.info(f"页面标题: {title}")
                
                # 检查是否有数据列表
                list_count = await page.locator('list').count()
                logger.info(f"找到列表数量: {list_count}")
                
                if list_count > 0:
                    item_count = await page.locator('list listitem').count()
                    logger.info(f"找到数据项数量: {item_count}")
                    
                    if item_count > 0:
                        # 获取第一条数据的文本
                        first_item_text = await page.locator('list listitem').first.text_content()
                        logger.info(f"第一条数据: {first_item_text[:100]}...")
                        
                        logger.info("✅ 网站访问和数据识别测试成功！")
                        return True
                
            logger.error("❌ 测试失败")
            return False
            
        except Exception as e:
            logger.error(f"测试异常: {e}")
            return False
        finally:
            await browser.close()


if __name__ == "__main__":
    result = asyncio.run(simple_test())
    exit(0 if result else 1)
